﻿#pragma once
namespace dt {
	// 仿函数/函数对象
	template<class T>
	class Less
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};

	
	class Date
	{
	public:
		Date(int year = 1900, int month = 1, int day = 1)
			: _year(year)
			, _month(month)
			, _day(day)
		{}
		bool operator<(const Date& d)const
		{
			return (_year < d._year) ||
				(_year == d._year && _month < d._month) ||
				(_year == d._year && _month == d._month && _day < d._day);
		}
		bool operator>(const Date& d)const
		{
			return (_year > d._year) ||
				(_year == d._year && _month > d._month) ||
				(_year == d._year && _month == d._month && _day > d._day);
		}
		friend ostream& operator<<(ostream& _cout, const Date& d)
		{
			_cout << d._year << "-" << d._month << "-" << d._day;
			return _cout;
		}
	private:
		int _year;
		int _month;
		int _day;
	};

	//类模板特化
	template<>
	struct Less<Date*>
	{
		bool operator()(const Date* x, const Date* y)
		{
			return *x < *y;
		}
	};

	template<class T>
	class Greater
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};
	template<class T, class Container = vector<T>, class Compare = Less<T>>
	class priority_queue
	{
	public:
		priority_queue()
		{}

		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
			:_con(first, last)
		{
			// ½¨¶Ñ
			for (int i = (_con.size() - 2) / 2; i >= 0; --i)
			{
				adjust_down(i);
			}
		}

		void adjust_up(int child)
		{
			Compare com;
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				//有名对象
				if (com(_con[parent], _con[child]))
				//匿名对象
				//if (Compare()(_con[parent], _con[child]))
					//if (_con[parent] < _con[child])
				{
					swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void adjust_down(int parent)
		{
			Compare com;
			size_t child = parent * 2 + 1;
			while (child < _con.size())
			{
				//if (child + 1 < _con.size() 
				//	//&& _con[child] < _con[child + 1])
				if (child + 1 < _con.size()
					&& com(_con[child], _con[child + 1]))
				{
					++child;
				}

				//if (_con[parent] < _con[child])
				if (com(_con[parent], _con[child]))
				{
					swap(_con[child], _con[parent]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}

		void push(const T& x)
		{
			_con.push_back(x);
			adjust_up(_con.size() - 1);
		}

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			adjust_down(0);
		}

		const T& top()
		{
			return _con[0];
		}

		bool empty()
		{
			return _con.empty();
		}

		size_t size()
		{
			return _con.size();
		}
	private:
		Container _con;
	};
	
	class PDateLess {
	public:
		bool operator()(const Date* p1, const Date* p2) {
			return *p1 < *p2;
		}
	};
	class PDateGreater{
	public:
		bool operator()(const Date* p1, const Date* p2) {
			return *p1 > *p2;
		}
	};
	void test_priority_queue() {
		priority_queue<int> pq;
		pq.push(1);
		pq.push(2);
		pq.push(3);
		pq.push(4);
		pq.push(1);

		while (!pq.empty()) {
			cout << pq.top() << " ";
			pq.pop();
		}
		cout << endl;
	}
	void test_priority_queue2()
	{
		// 大堆，需要用户在自定义类型中提供<的重载
		priority_queue<Date,vector<Date>,Less<Date>> q1;
		q1.push(Date(2018, 10, 29));
		q1.push(Date(2018, 10, 28));
		q1.push(Date(2018, 10, 30));
		cout << q1.top() << endl;
		priority_queue<Date*, vector<Date*>> q3;
		//priority_queue<Date*, vector<Date*>,PDateLess> q3;
		q3.push(new Date(2018, 10, 29));
		q3.push(new Date(2018, 10, 28));
		q3.push(new Date(2018, 10, 30));
		cout << *(q3.top()) << endl;
		// 如果要创建小堆，需要用户提供>的重载
		priority_queue<Date, vector<Date>, Greater<Date>> q2;
		q2.push(Date(2018, 10, 29));
		q2.push(Date(2018, 10, 28));
		q2.push(Date(2018, 10, 30));
		cout << q2.top() << endl;
	}
}